package com.hsyt.pms.controller.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.hsyt.pfc.support.rpc.PageResult;
import com.hsyt.pfc.support.util.EnumToDataUtil;
import com.hsyt.pfc.support.util.LoggerUtil;
import com.hsyt.pms.api.common.PltLog;
import com.hsyt.pms.api.common.ResultMessage;
import com.hsyt.pms.api.dto.system.SysUrlWhiteDto;
import com.hsyt.pms.api.enums.AbleStatus;
import com.hsyt.pms.api.enums.SysType;
import com.hsyt.pms.api.vo.system.SysUrlWhiteVo;
import com.hsyt.pms.controller.base.BaseController;
import com.hsyt.pms.sys.dubbo.SysUrlWhiteService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.MessageFormat;
import java.util.Date;

/**
 * @ClassName: SysWhiteController
 * @Description: 系统白名单服务控制层
 * @author: wangze@hsyuntai.com
 * @date: 2019/1/29 18:19
 */
@RestController
@RequestMapping("/pms/system/white")
public class SysWhiteController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysWhiteController.class);

    @Reference(group = "pmsSystem", registry = "pms", version = "0.0.1")
    private SysUrlWhiteService sysUrlWhiteService;

    @RequestMapping(value = "/page")
    public ModelAndView page(ModelMap model) {
        String sysType = EnumToDataUtil.getDHEnumsValuesFromEnum(SysType.class);
        model.put("sysType", sysType);
        return new ModelAndView("system/pageWhite", model);
    }

    @PltLog(log = "分页查询白名单")
    @RequestMapping(value = "/findPageData")
    public @ResponseBody
    PageResult findPageData(@RequestBody SysUrlWhiteDto dto) {
        PageResult<SysUrlWhiteVo> pageResult = new PageResult<SysUrlWhiteVo>();
        try {
            pageResult = sysUrlWhiteService.searchPageDataByDto(dto);
        } catch (Exception e) {
            pageResult.setResult(false);
            String message = MessageFormat.format("SysWhiteController.searchPageData error: {0}", e.getMessage());
            pageResult.setMessage(message);
            LoggerUtil.error(LOGGER, message);
        }
        return pageResult;
    }


    @RequestMapping(value = "/toEditForm")
    public ModelAndView toEditForm(ModelMap model, @RequestParam("formType") String formType) {
        model.put("formType", formType);
        return new ModelAndView("system/formWhite");
    }

    @PltLog(log = "新增URL白名单")
    @RequestMapping(value = "/add")
    public @ResponseBody
    ResultMessage add(@RequestBody SysUrlWhiteDto dto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "保存成功");
        try {
            dto.setId(null);
            if (this.sysUrlWhiteService.findPageDataCountByDto(dto) > 0) {
                result.change(false, new StringBuilder("当前系统白名单里该应用ID【").append(dto.getAppId()).append("】下已存在该URL,请修改！").toString());
                return result;
            }
            dto.setGmtCreate(new Date());
            dto.setGmtModify(new Date());
            String operator = getSessionPmsUser(request).getUserName();
            dto.setOperator(operator);
            dto.setStatus(AbleStatus.ENABLED.getCode());
            this.sysUrlWhiteService.insertSelective(dto);
        } catch (Exception e) {
            result.change(false, "保存失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysUrlWhiteListController-addSysUrl error:{0} param:{1}", e.getMessage(), dto);
        }
        return result;
    }

    @PltLog(log = "编辑URL白名单")
    @RequestMapping(value = "/edit")
    public @ResponseBody
    ResultMessage editS(@RequestBody SysUrlWhiteDto dto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "保存成功");
        try {
            if (this.sysUrlWhiteService.findPageDataCountByDto(dto) > 0) {
                result.change(false, new StringBuilder("当前系统白名单里该应用ID【").append(dto.getAppId()).append("】下已存在该URL,请修改！").toString());
                return result;
            }
            dto.setGmtModify(new Date());
            String operator = getSessionPmsUser(request).getUserName();
            dto.setOperator(operator);
            this.sysUrlWhiteService.updateByPrimaryKeySelective(dto);
        } catch (Exception e) {
            result.change(false, "保存失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysUrlWhiteListController-editSysUrl error:{0} param:{1}", e.getMessage(), dto);
        }
        return result;
    }

    @PltLog(log = "保存URL白名单")
    @RequestMapping(value = "/save")
    public @ResponseBody
    ResultMessage save(@RequestBody SysUrlWhiteDto dto, String formType, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "成功");
        try {
            if (dto.getId() == null) {
                if (this.sysUrlWhiteService.findPageDataCountByDto(dto) > 0) {
                    result.change(false, new StringBuilder("当前系统白名单里该应用ID【").append(dto.getAppId()).append("】下已存在该URL,请修改！").toString());
                    return result;
                }
                dto.setGmtCreate(new Date());
                dto.setGmtModify(new Date());
                dto.setStatus(AbleStatus.ENABLED.getCode());
                this.sysUrlWhiteService.insertSelective(dto);
            } else {
                if (this.sysUrlWhiteService.findPageDataCountByDto(dto) > 0) {
                    result.change(false, new StringBuilder("当前系统白名单里该应用ID【").append(dto.getAppId()).append("】下已存在该URL,请修改！").toString());
                    return result;
                }
                dto.setGmtModify(new Date());
                this.sysUrlWhiteService.updateByPrimaryKeySelective(dto);
            }
        } catch (Exception e) {
            result.change(false, "保存失败 , 错误信息:" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysWhiteController-save error: {0}", e.getMessage());
        }
        return result;
    }

    @PltLog(log = "白名单启用/停用状态")
    @RequestMapping(value = "/doUndo")
    public @ResponseBody
    ResultMessage doUndo(@RequestBody SysUrlWhiteDto dto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "成功");
        if (dto.getId() == null) {
            result.change(false, "参数中角色id为空");
            return result;
        }
        if (StringUtils.isBlank(dto.getStatus())) {
            dto.setStatus(AbleStatus.ENABLED.getCode());
        }
        try {
            sysUrlWhiteService.updateByPrimaryKeySelective(dto);
        } catch (Exception e) {
            result.change(false, "操作失败,错误信息:" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysRoleController doUndo error: {0}", e.getMessage());
        }
        return result;
    }

    @PltLog(log = "删除URL白名单")
    @RequestMapping(value = "/delSysUrl")
    public @ResponseBody
    ResultMessage delSysUrl(@RequestBody SysUrlWhiteDto dto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "删除成功");
        try {
            dto.setStatus(AbleStatus.DISABLED.getCode());
            dto.setGmtModify(new Date());
            String operator = getSessionPmsUser(request).getUserName();
            dto.setOperator(operator);
            this.sysUrlWhiteService.updateByPrimaryKeySelective(dto);
        } catch (Exception e) {
            result.change(false, "删除失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysUrlWhiteListController-delSysUrl error:{0} param:{1}", e.getMessage(), dto);
        }
        return result;
    }

}
